package com.xhui.vimarket.zkplus.zul.attr.mod.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.directwebremoting.util.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Box;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;

import com.xhui.crm.common.util.string.StrUtil;
import com.xhui.crm.common.vo.RetVO;com.xhui.vimarket.zkplusui.vimarket.zkplus.ui.id.ZKRebcom.xhui.vimarket.zkplusrt com.xhui.vimarket.zkpcom.xhui.vimarket.zkplusls;
import com.xhui.vimcom.xhui.vimarket.zkplusil.ZkUiUtil;
import com.xhcom.xhui.vimarket.zkpluslus.zul.attr.AttrExt;
import com.xhui.vcom.xhui.vimarket.zkpluszul.attr.ada.AttrExecListener;
icom.xhui.vimarket.zkplusvimarket.zkplus.zul.attr.bean.Attcom.xhui.vimarket.zkpluscom.xhui.vimarket.zkplus.zul.atcom.xhui.vimarket.zkplusl;
import com.xhui.vimarket.zkplus.zul.acom.xhui.vimarket.zkplus;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompConstants;
imcom.xhui.vimarket.zkplusimarket.zkplus.zul.attr.mod.AttrCompUtil;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrCheckboxComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrCheckboxComp extends AttrBaseComp {
    
    /**
     * .
     */
    public static final String      STYLE_CHECKBOX_LABEL = "checkboxlabel";
    
    /**
     * .
     */
    public static final String      STYLE_LABEL_CHECKBOX = "labelcheckbox";
    
    /**
     * .
     */
    private static final String     HORIZONTAL           = "horizontal";
    
    /**
     * .
     */
    private static final String     VERTICAL             = "vertical";
    
    /**
     * 实例.
     */
    private static AttrCheckboxComp inst                 = new AttrCheckboxComp();
    
    /**
     * 日志.
     */
    private static Logger           logger               = Logger.getLogger(AttrCheckboxComp.class);
    
    /**
     * 构造函数. 
     */
    private AttrCheckboxComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrCheckboxComp instance() {
        return AttrCheckboxComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @SuppressWarnings("deprecation")
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // box
        final Box box = new Box();
        box.setOrient(AttrCheckboxComp.VERTICAL);
        box.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        box.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        box.setAttribute(AttrBaseComp.COMP_JAVACODE, "" + attrTmpl.getJavaCode());
        box.setAttribute(AttrBaseComp.COMP_CANTNULL, AttrCompUtil.getStrCantNull(attrTmpl
            .isCantNull()));
        box.setAttribute(AttrBaseComp.COMP_COMP_NAME, attrTmpl.getName());
        AttrCompUtil.setCompAttr(box, AttrBaseComp.COMP_VALUE_LENGTH, attrTmpl.getValueLength());
        AttrCompUtil.setCompAttr(box, AttrBaseComp.COMP_VALUE_FROM, attrTmpl.getValueFrom());
        AttrCompUtil.setCompAttr(box, AttrBaseComp.COMP_VALUE_TO, attrTmpl.getValueTo());
        AttrCompUtil.setCompAttr(box, AttrBaseComp.COMP_VALUE_REX, attrTmpl.getValidExpr());
        //box.setWidth("100%");
        box.setStyle("width:100%;_width:auto;_display:block;");
        box.setParent(comp);
        if (attrTmpl.getHint() != null) {
            box.setTooltiptext(attrTmpl.getHint());
        }
        if (attrTmpl.getDefProdOfferId() != 0) {//如果产品存在多个默认销售品时，设置销售品ID
            AttrCompUtil.setCompAttr(box, AttrBaseComp.COMP_DEFPO_ID, attrTmpl.getDefProdOfferId());
        }
        // box // box
        final Box subBox = new Box();
        subBox.setOrient(AttrCheckboxComp.HORIZONTAL);
        subBox.setClass("zk-eligible");
        subBox.setParent(box);
        //subBox.setWidth("100%");
        subBox.setStyle("width:100%;_width:auto");
        // box // box // labelDiv
        final Div labDiv = new Div();
        Label label = new Label();
        label.setValue(attrTmpl.getName());
        label.setParent(labDiv);
        label.setSclass("zp-lt-label");
        // info
        this.createRulebar(attrExt, labDiv, attrTmpl);
        labDiv.setParent(subBox);
        // box // box // checkbox
//        final String sCheckboxWithComposer = attrExt.getAttrAda().getAttrTmplCompStr(
//            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null);
//        final Checkbox checkbox = (Checkbox) Executions.createComponentsDirectly(
//            sCheckboxWithComposer, null, subBox, null);
        final Checkbox checkbox = (Checkbox) attrExt.getAttrAda().getAttrTmplCompStr(
            attrTmpl.getDisplayMode(), attrTmpl, attrTmpl.getParam2(), null, null, subBox);
        //重构Uuid用于压力测试。
        ZKRebuildUuid.rebuildUuidById(checkbox, "cc_"
            + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId()
                : "_" + Math.abs(attrTmpl.getAttrId())));
        // toolbox
        this.createToolbox(subBox, attrExt.isToolboxVisible(), attrTmpl);
        // re set
        if (STYLE_CHECKBOX_LABEL.equals(attrTmpl.getDisplayStyle() + "")) { // label跟在复选框后面
            checkbox.setLabel(attrTmpl.getName());
            label.setVisible(false);
        } else {
            subBox.setHflex("true");
            subBox.setWidths(this.getWidthsLCT(attrTmpl));
            //subBox.setSclass("zh-hbox-widthc");
            subBox.setStyle("width:100%;");
            checkbox.setLabel(attrTmpl.getName());           
            label.setVisible(false);
        }
        if (attrExt.getAttrAda().isViewMode()) { // 展示模式
            checkbox.setDisabled(true);
        } else if (attrTmpl.isReadOnly()) {
            checkbox.setDisabled(true);
        }
        // comp log
        if (AttrCheckboxComp.logger.isDebugEnabled()) {
            attrExt.appendInnerComp("<box orient=\"vertical\">");
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_ATTRID, "" + attrTmpl.getAttrId());
            attrExt.appendInnerCompAttr(AttrBaseComp.COMP_JAVACODE, "" + attrTmpl.getJavaCode());
            attrExt.appendInnerComp("<box orient=\"herizontal\">");
            attrExt.appendInnerComp("<checkbox label=\"" + attrTmpl.getName() + "\" />");
            attrExt.appendInnerComp("</box>");
            attrExt.appendInnerComp("</box>");
        }
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList,Map<String, Integer> attrViewMap) {
        super.setValue(attrExt, comp, attrInstList,attrViewMap);
        // 取输入框
        final Component compCheckbox = AttrCheckboxComp.getCheckbox(comp);
        if (compCheckbox != null) {
            final long theAttrId = NumUtils
                .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
            final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                theAttrId);
            if (theAttrList.size() > 0) {
                if (!StrUtil.isNullOrEmpty(theAttrList.get(0).getParam1())) { // 有值才代表选中
                    ((Checkbox) compCheckbox).setChecked(true);
                    if (theAttrList.get(0).getData() != null) {
                        AttrCompUtil.bindData(theAttrList.get(0).getData(), comp);
                    }
                    Events.sendEvent("onSetValue", compCheckbox, theAttrList.get(0));
                    // javacode回填
                    AttrCompUtil.copyTmplInfo(theAttrList.get(0), comp);
                } else {
                    ((Checkbox) compCheckbox).setChecked(false);
                }
            }
        }
        // diasbled 设置
        tcom.xhui.vimarket.zkplusValue(attrExt, comp, attrInstList);
    }
    com.xhui.vimarket.zkplus * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setRemovedValue(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.util.List)
     * @author wuyx
     * 2011-4-8 wuyx
     */
    public void setRemovedValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList) {
        super.setRemovedValue(attrExt, comp, attrInstList);
        // 取输入框
        final Component compCheckbox = AttrCheckboxComp.getCheckbox(comp);
        if (compCheckbox != null) {
            final long theAttrId = NumUtils
                .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
            final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                theAttrId);
            if (theAttrList.size() > 0) {
                if (!((Checkbox) compCheckbox).isChecked()) {
                    ((Checkbox) compCheckbox).setSclass("zp-removed");
                }
                if (theAttrList.get(0).getData() != null) {
                    AttrCompUtil.bindData(theAttrList.get(0).getData(), comp);
                }
                comp.setAttribute(AttrBaseComp.COMP_REMOVED, AttrBaseComp.COMP_REMOVED);
                Events.sendEvent("onSetRemovedValue", compCheckbox, theAttrList.get(0));
                // javacode回填
                AttrCompUtil.copyTmplInfo(theAttrList.get(0), comp);
            }
        }
        // diascom.xhui.vimarket.zkplus  this.setDisabledValue(attrExt, comp, attrInstLicom.xhui.vimarket.zkplus  
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setDisabledValue(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component, java.util.List)
     * @author wuyx
     * 2011-4-8 wuyx
     */
    public void setDisabledValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList) {
        super.setRemovedValue(attrExt, comp, attrInstList);
        // 取输入框
        final Component compCheckbox = AttrCheckboxComp.getCheckbox(comp);
        if (compCheckbox != null) {
            final long theAttrId = NumUtils
                .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID));
            final ArrayList<AttrInst> theAttrList = AttrBaseComp.getAttrInstListById(attrInstList,
                theAttrId);
            if (theAttrList.size() > 0) {
                if (theAttrList.get(0).isDisabled()) {
                    this.setDisabled(attrExt, comp, true);
                }
            }
        }
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 取输入框
        final Component compCheckbox = AttrCheckboxComp.getCheckbox(comp);
        if (compCheckbox != null) {
            if (((Checkbox) compCheckbox).isChecked()) { // 有填
                final AttrInst attrInst = attrExt.newAttrInst();
                attrInst.setAttrInstId(AttrCompUtil.getAttrCompAttrInstId(comp));
                attrInst.setAttrId(NumUtils
                    .toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
                attrInst.setParam1("1");
                attrInst
                    .setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
                attrInst.setOp(this.getToolbarOp(comp));
                retList.add(attrInst);
                attrInst.setDacom.xhui.vimarket.zkplus.getAttrInstData(comp));
            }
    com.xhui.vimarket.zkplusreturn retList;
    }

    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        if (AttrCompUtil.isCantNull(comp)) { // 必填
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) {
                ValiVo retVo = new ValiVo();
                ArrayList<ValiVo> retVoList = new ArrayList<ValiVo>();
                retVoList.add(retVo);
                return retVoList;
            }
        }
        return null;
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        // 取输入框
        final Component compCheckbox = AttrCheckboxComp.getCheckbox(comp);
        if (compCheckbox != null) {
            final AttrInst attrInst = attrExt.newAttrInst();
            attrInst.setAttrInstId(AttrCompUtil.getAttrCompAttrInstId(comp));
            attrInst.setAttrId(NumUtils.toLong("" + comp.getAttribute(AttrBaseComp.COMP_ATTRID)));
            attrInst.setParam1(null);
            attrInst.setJavaCode(StrUtil.strnull(comp.getAttribute(AttrBaseComp.COMP_JAVACODE)));
            attrInst.setOp(this.getToolbarOp(comp));
            retList.add(attrInst);
            attrInst.setData(AttrCompUtil.getAttrInstData(comp));
            
        }
        return retList;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        final ArrayList<AttrInst> retList = new ArrayList<AttrInst>();
        final AttrInst attrInst = attrExt.newAttrInst();
        attrInst.setAttrId(attrTmpl.getAttrId());
        attrInst.setJavaCode(attrTmpl.getJavaCode());
        retList.add(attrInst);
        return retList;
    }
    
    /**
     * 清空值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     */
    @Override
    public void clean(final AttrExt attrExt, final Component comp) {
        super.clean(attrExt, comp);
        // 取输入框
        final Component compCheckbox = AttrCheckboxComp.getCheckbox(comp);
        if (compCheckbox != null) {
            ((Checkbox) compCheckbox).setChecked(false);
        }
    }
    
    /**
     * getCheckbox.
     * 
     * @param comp Component
     * @return Checkbox
     * @author wuyx
     * 2011-1-29 wuyx 
     */
    public static Checkbox getCheckbox(final Component comp) {
        return (Checkbox) comp.getFirstChild().getFirstChild().getNextSibling();
    }
    
    /**
     * 获取根控件.
     * 
     * @param comp Component
     * @return Component
     * @author wuyx
     * 2011-1-29 wuyx 
     */
    public static Component getRootCompByCheckbox(final Component comp) {
        return comp == null ? null
            : comp.getParent().getParent();
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
        super.setDisabled(attrExt, comp, tflag);
        final Component compCheckbox = AttrCheckboxComp.getCheckbox(comp);
        ZkUiUtil.setDisabled(compCheckbox, tflag);
    }
    
    /**
     * getToolbarOp.
     * 
     * @param parentComp Component
     * @return String
     * @author wuyx
     * 2011-3-1 wuyx 
     */
    @SuppressWarnings("unchecked")
    public String getToolbarOp(Component parentComp) {
        if (parentComp != null) {
            List<Component> subComps = parentComp.getChildren();
            if (subComps != null) {
                for (Component subComp : subComps) {
                    if (subComp.hasAttribute("toolbar")) { // 工具按钮
                        return subComp.getFirstChild().getAttribute("op") + "";
                    }
                    // 下级
                    String subOp = this.getToolbarOp(subComp);
                    if (subOp != null) {
                       com.xhui.vimarket.zkplus
                    }
             com.xhui.vimarket.zkplus   }
        }
        return null;
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp#
     * exec(com.xhui.vimarket.zkplus.zul.attr.ada.AttrExecListener)
     * @author wuyx
     * 2011-3-16 wuyx
     */
    @Override
    public void exec(final AttrExt attrExt, final Component comp, AttrExecListener attrExecListener) {
        super.exec(attrExt, comp, attrExecListener);com.xhui.vimarket.zkplustrExecListener == null) {
            return;
  com.xhui.vimarket.zkplus  attrExecListener.onEvent(comp, attrExt);
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp
     * #validate(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-3-22 wuyx
     */
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        if (AttrCompUtil.isCantNull(comp)) { // 需限制不可空
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) { // 没有值
                return AttrCompUtil.getRetVoList(AttrCompConstants.RET_ERROR, AttrCompUtil
                    .getCompName(comp)
                    + AttrCompConstants.STRING_CANT_NULL, comp);
            }
            return null; // 通过
        }
        return null; // 通过
    }
    
    /**
     * getRemovedObject.
     * 
     * @param rootComp Component
     * @return Object
     * @author wuyx
     * 2011-4-8 wuyx 
     */
    public static Object getRemovedObject(Component rootComp) {
        if (rootComp != null && rootComp.hasAttribute(Acom.xhui.vimarket.zkplusP_DATA)) {
            return rootComp.getAttribute(AttrBaseComp.COMP_DATA);
        }
        return null;
    }
    
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */
    public void setAttrView(final Component comp, Map<String, Integer> attrViewMap) {
        if (attrViewMap != null && attrViewMap.size() > 0) {
            String attrJC = comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "";     
            if (!StrUtil.isNullOrEmpty(attrJC)) {
                Integer viewType = attrViewMap.get(attrJC);
                // 取输入框
                final Checkbox checkbox = getCheckbox(comp);
                if (checkbox != null) {
                    checkbox.setVisible(false);
                    if (AttrBaseComp.VIEW_TYPE_SHOW == viewType) {
                        checkbox.setDisabled(true);
                        checkbox.setVisible(true);
                    } else if (AttrBaseComp.com.xhui.vimarket.zkplus== viewType) {
                        checkbox.setVisible(true);
                    }
                }
            }
        }
    }
    
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #getMsgComp(org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2011-7-19 wuyx
     */
    @Override
    public Component getMsgComp(Component comp) {
        return getCheckbox(comp);
    }
}
